package cn.zcy.controller.intercept;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckInterceptorController implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (StringUtils.isEmpty(request.getSession().getAttribute("username"))) {
            System.out.println("检验是否登录的拦截器执行了");
            StringBuffer requestURL = request.getRequestURL();
            System.out.println("从哪个请求来的="+requestURL);
            request.getSession().setAttribute("url01",requestURL);
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        } else {
            return true;
        }
    }
}
